// 引入windi css
import '@/plugins/windi.css'

// 导入全局的svg图标
import '@/plugins/svgIcon'

// 初始化多语言
import { setupI18n } from '@/plugins/vueI18n'

// 引入状态管理
import { setupStore } from '@/store'

// 全局组件
import { setupGlobCom } from '@/components'

// 引入element-plus
import { setupElementPlus } from '@/plugins/elementPlus'

// 引入全局样式
import '@/styles/index.less'

// 引入动画
import '@/plugins/animate.css'

// element css
import "element-plus/dist/index.css";
// element dark(内置暗黑模式)
import "element-plus/theme-chalk/dark/css-vars.css";

// 路由
import { setupRouter } from './router'
import router from "./router"

// element plus
import ElementPlus from "element-plus";
// 权限
import { setupPermission } from './directives'

import { createApp } from 'vue'

import App from './App.vue'

import './permission'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import {
  renderWithQiankun,
  qiankunWindow,
  type QiankunProps,
} from "vite-plugin-qiankun/dist/helper";

// 创建实例
const setupAll = async (props: QiankunProps = {}) => {
  const { container,onGlobalStateChange } = props;
  const el: string | Element =
    container?.querySelector("#opinion-sys") || "#opinion-sys";
  const app = createApp(App)

  if(onGlobalStateChange){
    props.onGlobalStateChange((state:any) => {
      console.log('通信状态发生改变：', state);
      let {DoGis,token,userInfo} = state;
      app.config.globalProperties.$DoGis = DoGis;
      app.config.globalProperties.$token = token;
      app.config.globalProperties.$userInfo = userInfo;
    }, true);
  }

  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
  }
  app.use(ElementPlus)

  await setupI18n(app)

  setupStore(app)

  setupGlobCom(app)

  setupElementPlus(app)

  setupRouter(app)

  setupPermission(app)

  app.mount(el)

  
}
const initQianKun = () => {
  renderWithQiankun({
    bootstrap() {
      console.log("微应用:bootstrap");
    },
    mount(props) {
      // 获取主应用传入数据
      console.log("微应用:mount", props);
      setupAll(props);
    }, 
    unmount(props) {
      console.log("微应用:unmount", props);
      setTimeout(() => {
        location.reload();
      }, 0);
    },
    update(props) {
      console.log("微应用:update", props);
    },
  });
};

qiankunWindow.__POWERED_BY_QIANKUN__ ? initQianKun() : setupAll();
